package com.dunshan.data.controller;

import com.alibaba.fastjson.JSON;
import com.dunshan.data.mapper.UserMapper;
import com.dunshan.data.pojo.UmsAdmin;
import com.dunshan.data.service.Userser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.HashMap;

/**
 * @author liwen406
 * @program: dunshan-pef
 * @description: 请求
 * @date 2021-07-17 16:48:14
 */
@Controller
public class IndexController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    Userser userser;

    /**
     * 在请求url中增加
     *
     * @param master
     * @return
     */
    @GetMapping("/index")
    @ResponseBody
    public Object index(@PathParam("master") String master) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ok", "test");
        map.put("master", "test");
        map.put("data", userMapper.selectByMap(null));

        return map;
    }


    /**
     * 在请求头信息增加
     *
     * @param user
     * @param request
     * @return
     */
    @GetMapping("/header")
    @ResponseBody
    public Object index(UmsAdmin user, HttpServletRequest request) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ok", JSON.toJSON(user));
        map.put("master", "test");
        map.put("data", userMapper.selectByMap(null));
        return map;
    }


    @GetMapping("/user")
    @ResponseBody
    public Object TestUser(@PathParam("master") String master) {

        HashMap<Object, Object> map = new HashMap<>();
        map.put("slave", userser.slave());
        map.put("master", userser.master());

        return map;
    }

    @GetMapping("/sql")
    @ResponseBody
    public Object sqltest(UmsAdmin user) {
        HashMap<String, Object> map = new HashMap<>();
        return userMapper.selectByMap(map);
    }


    /**
     * 在请求头信息增加
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    public Object inserts(@RequestBody UmsAdmin user, HttpServletRequest request) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ok", JSON.toJSON(user));
        map.put("插入成功", userMapper.insert(user));
        return map;
    }

}
